package com.ruoyi.file.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.file.domain.TblFileData;
import com.ruoyi.file.mapper.TblFileDataMapper;
import com.ruoyi.file.service.ITblFileDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
/**
 * 文件数据Service业务层处理
 *
 * @author rww
 * @date 2023-11-13
 */
@Service
public class TblFileDataServiceImpl implements ITblFileDataService
{
    @Autowired
    private TblFileDataMapper tblFileDataMapper;
    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 查询文件数据
     *
     * @param id 文件数据主键
     * @return 文件数据
     */
    @Override
    public TblFileData selectTblFileDataById(Long id)
    {
        return tblFileDataMapper.selectTblFileDataById(id);
    }

    /**
     * 查询文件数据列表
     *
     * @param tblFileData 文件数据
     * @return 文件数据
     */
    @Override
    public List<TblFileData> selectTblFileDataList(TblFileData tblFileData)
    {
        return tblFileDataMapper.selectTblFileDataList(tblFileData);
    }

    /**
     * 新增文件数据
     *
     * @param tblFileData 文件数据
     * @return 结果
     */
    @Override
    public int insertTblFileData(TblFileData tblFileData)
    {
        return tblFileDataMapper.insertTblFileData(tblFileData);
    }

    /**
     * 修改文件数据
     *
     * @param tblFileData 文件数据
     * @return 结果
     */
    @Override
    public int updateTblFileData(TblFileData tblFileData)
    {
        return tblFileDataMapper.updateTblFileData(tblFileData);
    }

    /**
     * 批量删除文件数据
     *
     * @param ids 需要删除的文件数据主键
     * @return 结果
     */
    @Override
    public int deleteTblFileDataByIds(Long[] ids)
    {
        return tblFileDataMapper.deleteTblFileDataByIds(ids);
    }

    /**
     * 删除文件数据信息
     *
     * @param id 文件数据主键
     * @return 结果
     */
    @Override
    public int deleteTblFileDataById(Long id)
    {
        return tblFileDataMapper.deleteTblFileDataById(id);
    }

    @Override
    public void deleteDataByFileId(Long fileId) {
        tblFileDataMapper.deleteDataByFileId(fileId);
    }

    @Override
    public List<Map<String, Object>> selectTblFileDataByIdAndField(Long fileId, String field) {
        //a.file_info::json->>#{field}
        String selectField = "a.file_info::json->>'"+field+"'";
        String sql = "SELECT   " + selectField+ " as field ,count(1) as num "+
                "  FROM tbl_file_data a " +
                "  where a.file_id = " +fileId +
                "  GROUP BY  "+selectField + "   ORDER BY num desc " +
                " limit 100 ";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
//        return  tblFileDataMapper.selectTblFileDataByIdAndField(sql);
        return maps;
    }
}
